<template>
  <div class="page-container">
    <head-component></head-component>
    <div class="main-container">
      <aside-component></aside-component>
      <div class="content-container">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>商品管理</span>
              <div class="search-inputs">
                <el-input
                  v-model="searchQuery"
                  placeholder="请输入商品名称或价格"
                  clearable
                  style="width: 250px; margin-right: 10px;"
                  @keyup.enter="handleSearch"
                />
                <el-button type="primary" @click="handleSearch">搜索</el-button>
                <el-button type="success" @click="handleAdd" style="margin-left: 10px;">新增商品</el-button>
              </div>
            </div>
          </template>
          <el-table :data="products.list" style="width: 100%" v-loading="loading" empty-text="暂无商品数据">
            <el-table-column label="商品图片" width="100">
              <template #default="scope">
                <div class="product-preview">
                  <el-image
                    v-if="scope.row.imageLoaded"
                    style="width: 60px; height: 60px"
                    :src="scope.row.imageUrl"
                    fit="cover"
                    class="preview-image"
                    @click="handlePreview(scope.row)">
                    <template #error>
                      <div class="image-error">
                        <el-icon><Picture /></el-icon>
                        <span>加载失败</span>
                      </div>
                    </template>
                  </el-image>
                  <span v-else class="loading-text">加载中...</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="productName" label="商品名称" width="150">
              <template #default="scope">
                <span v-html="highlightMatch(scope.row.productName, searchQuery)"></span>
              </template>
            </el-table-column>
            <el-table-column prop="productType" label="商品分类" width="120">
              <template #default="scope">
                {{ getCategoryName(scope.row.productType) }}
              </template>
            </el-table-column>
            <el-table-column prop="productDesc" label="商品描述" width="200" show-overflow-tooltip />
            <el-table-column prop="productPrice" label="价格" width="100">
              <template #default="scope">
                <span v-html="highlightMatch(scope.row.productPrice, searchQuery)"></span>
              </template>
            </el-table-column>
            <el-table-column prop="productStock" label="库存" width="100" />
            <el-table-column prop="productSales" label="销量" width="100" />
            <el-table-column prop="productClick" label="点击量" width="100" />
            <el-table-column prop="productStatus" label="状态" width="100">
              <template #default="scope">
                <el-tag :type="scope.row.productStatus === 1 ? 'success' : 'danger'">
                  {{ scope.row.productStatus === 1 ? '上架' : '下架' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="productColor" label="颜色" width="100" />
            <el-table-column prop="productSize" label="尺寸" width="100" />
            <el-table-column prop="productWeight" label="重量(g)" width="100" />
            <el-table-column prop="productMaterial" label="材质" width="100" />
            <el-table-column prop="createTime" label="创建时间" width="160" />
            <el-table-column prop="updateTime" label="更新时间" width="160" />
            <el-table-column label="操作" fixed="right" width="150">
              <template #default="scope">
                <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
                <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <div class="pagination-container">
            <el-pagination
              v-model:current-page="currentPage"
              v-model:page-size="pageSize"
              :page-sizes="[6, 10, 15, 20, 50]"
              :total="products.total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-card>
      </div>
    </div>
    <foot-component></foot-component>
  </div>

  <!-- 编辑/新增商品对话框 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" width="600px">
    <el-form :model="currentProduct" :rules="editRules" ref="editProductForm" label-width="100px">
      <!-- 必填字段 -->
      <el-form-item label="商品图片" prop="productImg">
        <div class="image-uploader" @click="triggerEditFileInput">
          <img v-if="currentProduct.imageUrl" :src="currentProduct.imageUrl" class="product-image" alt="商品图片">
          <div v-else class="image-uploader-icon">
            <el-icon><Plus /></el-icon>
            <span class="upload-text">点击上传图片</span>
            <div class="image-uploader-info">
              <p>支持 JPG、PNG 格式</p>
              <p>文件小于 2MB</p>
            </div>
          </div>
          <div v-if="currentProduct.imageUrl" class="image-uploader-mask">
            <span class="upload-text">点击更换图片</span>
          </div>
          <input
            type="file"
            ref="editFileInput"
            @change="handleEditImageChange"
            accept="image/jpeg,image/png"
            style="display: none"
          >
        </div>
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input v-model="currentProduct.productName" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品分类" prop="productType">
        <el-select v-model="currentProduct.productType" placeholder="请选择商品分类" style="width: 100%">
          <el-option
            v-for="item in categories"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品价格" prop="productPrice">
        <el-input-number v-model="currentProduct.productPrice" :min="0" :precision="2" :step="0.1" placeholder="请输入商品价格"></el-input-number>
      </el-form-item>
      <el-form-item label="商品库存" prop="productStock">
        <el-input-number v-model="currentProduct.productStock" :min="0" :step="1" placeholder="请输入商品库存"></el-input-number>
      </el-form-item>
      <el-form-item label="商品状态" prop="productStatus">
        <el-select v-model="currentProduct.productStatus" placeholder="请选择状态">
          <el-option label="上架" :value="1"></el-option>
          <el-option label="下架" :value="0"></el-option>
        </el-select>
      </el-form-item>

      <!-- 选填字段 -->
      <el-form-item label="商品描述" prop="productDesc">
        <el-input type="textarea" v-model="currentProduct.productDesc" :rows="3" placeholder="请输入商品描述"></el-input>
      </el-form-item>
      <el-form-item label="商品销量" prop="productSales">
        <el-input-number v-model="currentProduct.productSales" :min="0" :step="1" placeholder="请输入商品销量"></el-input-number>
      </el-form-item>
      <el-form-item label="商品点击量" prop="productClick">
        <el-input-number v-model="currentProduct.productClick" :min="0" :step="1" placeholder="请输入商品点击量"></el-input-number>
      </el-form-item>
      <el-form-item label="商品颜色" prop="productColor">
        <el-input v-model="currentProduct.productColor" placeholder="请输入商品颜色"></el-input>
      </el-form-item>
      <el-form-item label="商品尺寸" prop="productSize">
        <el-input v-model="currentProduct.productSize" placeholder="请输入商品尺寸"></el-input>
      </el-form-item>
      <el-form-item label="商品重量" prop="productWeight">
        <el-input-number v-model="currentProduct.productWeight" :min="0" :step="1" placeholder="请输入商品重量">
          <template #suffix>g</template>
        </el-input-number>
      </el-form-item>
      <el-form-item label="商品材质" prop="productMaterial">
        <el-input v-model="currentProduct.productMaterial" placeholder="请输入商品材质"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </span>
    </template>
  </el-dialog>

  <!-- 添加图片预览组件 -->
  <el-image-viewer
    v-if="previewVisible"
    :url-list="[previewUrl]"
    :initial-index="0"
    :hide-on-click-modal="false"
    :teleported="true"
    @close="previewVisible = false"
  />
</template>

<script>
import { ElMessage, ElMessageBox, ElImageViewer } from 'element-plus'
import AsideComponent from '../components/Aside.vue'
import HeadComponent from '../components/Head.vue'
import FootComponent from '../components/Foot.vue'
import defaultProductImg from '@/assets/no_image.jpg'
import { Plus, Picture } from '@element-plus/icons-vue'

export default {
  name: 'ProductManger',
  components: {
    AsideComponent,
    HeadComponent,
    FootComponent,
    Plus,
    Picture,
    ElImageViewer
  },

  data () {
    return {
      products: {
        total: 0,
        list: [],
        pageSize: 6,
        pageNum: 1
      },
      loading: false,
      currentPage: 1,
      pageSize: 6,
      searchQuery: '',
      defaultProductImg,
      dialogVisible: false,
      dialogTitle: '',
      currentProduct: {},
      editRules: {
        productName: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        productType: [
          { required: true, message: '请选择商品分类', trigger: 'change' }
        ],
        productPrice: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        productStock: [
          { required: true, message: '请输入商品库存', trigger: 'blur' }
        ],
        productStatus: [
          { required: true, message: '请选择商品状态', trigger: 'change' }
        ],
        // 选填字段的验证规则
        productDesc: [],
        productSales: [],
        productClick: [],
        productColor: [],
        productSize: [],
        productWeight: [],
        productMaterial: [],
        productImg: []
      },
      previewVisible: false,
      previewUrl: '',
      selectedImageFile: null, // 用于存储待上传的图片文件
      categories: []
    }
  },

  mounted () {
    this.getAllProducts()
    this.selectCategory()
  },

  watch: {
    searchQuery (newValue, oldValue) {
      if (!newValue && oldValue) {
        this.currentPage = 1
        this.getAllProducts()
      }
    }
  },

  methods: {
    // 异步加载商品图片
    async loadProductImage (product) {
      product.imageLoaded = false // 初始化加载状态
      product.imageUrl = '' // 初始化图片 URL

      const filename = product.productImg
      if (!filename || filename.trim() === '') {
        product.imageUrl = this.defaultProductImg
        product.imageLoaded = true
        return
      }

      try {
        const url = `/photo/view?filename=${encodeURIComponent(filename)}`
        const response = await this.$ajax.get(url, { responseType: 'blob' })
        const reader = new FileReader()
        reader.onloadend = () => {
          product.imageUrl = reader.result
          product.imageLoaded = true
        }
        reader.readAsDataURL(response.data)
      } catch (error) {
        console.error('Error loading product image:', error)
        product.imageUrl = this.defaultProductImg
        product.imageLoaded = true
      }
    },

    getAllProducts () {
      this.loading = true
      this.$ajax.get('/admin/product/selectAllProduct', {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize
        }
      })
        .then((response) => {
          const resp = response.data
          if (resp.code === 0) {
            this.products = { ...resp.data }
            this.currentPage = resp.data.pageNum
            this.pageSize = resp.data.pageSize

            if (this.products.list && this.products.list.length > 0) {
              this.products.list.forEach(product => {
                this.loadProductImage(product)
              })
            }
          } else {
            ElMessage.error(resp.msg)
          }
        })
        .catch((error) => {
          console.error('获取商品列表失败:', error)
          ElMessage.error('获取商品列表失败')
        })
        .finally(() => {
          this.loading = false
        })
    },

    handleSizeChange (val) {
      this.pageSize = val
      this.getAllProducts()
    },

    handleCurrentChange (val) {
      this.currentPage = val
      this.getAllProducts()
    },

    handleSearch () {
      this.currentPage = 1 // 搜索时重置到第一页
      // 判断搜索关键字是否为空
      if (!this.searchQuery) {
        ElMessage.warning('请输入关键字！')
        return
      }

      this.loading = true
      // 先清空当前列表，避免显示旧数据
      this.products.list = []

      this.$ajax.get('/admin/product/selectLikeProductNameAndPrice', {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.searchQuery
        }
      })
        .then((response) => {
          const resp = response.data
          if (resp.code === 0) {
            // 只有在有数据时才更新列表
            if (resp.data.list && resp.data.list.length > 0) {
              this.products = { ...resp.data }
              this.currentPage = resp.data.pageNum
              this.pageSize = resp.data.pageSize
              this.products.list.forEach(product => {
                this.loadProductImage(product)
              })
            } else {
              // 没有搜索结果时，只更新总数和分页信息
              this.products = {
                total: 0,
                list: [],
                pageSize: this.pageSize,
                pageNum: 1
              }
            }
          } else {
            ElMessage.error(resp.msg)
            // 发生错误时也清空列表
            this.products.list = []
          }
        })
        .catch((error) => {
          console.error('搜索商品失败:', error)
          ElMessage.error('搜索商品失败')
          // 发生错误时清空列表
          this.products.list = []
        })
        .finally(() => {
          this.loading = false
        })
    },

    handleAdd () {
      this.dialogVisible = true
      this.dialogTitle = '新增商品'
      // 初始化空商品数据
      this.currentProduct = {
        productId: null,
        productName: '',
        productDesc: '',
        productPrice: 0,
        productStock: 0,
        productSales: 0,
        productClick: 0,
        productStatus: 1, // 默认上架
        productImg: '',
        productColor: '',
        productSize: '',
        productWeight: 0,
        productMaterial: '',
        productType: '', // 添加分类字段
        imageUrl: '' // 新增商品时，图片URL初始化为空
      }
      // 重置表单验证状态
      if (this.$refs.editProductForm) {
        this.$refs.editProductForm.resetFields()
      }
    },

    handleEdit (row) {
      this.dialogVisible = true
      this.dialogTitle = '编辑商品信息'
      // 复制商品数据到 currentProduct
      this.currentProduct = {
        ...row,
        imageUrl: row.imageUrl || this.defaultProductImg
      }
      // 重置表单验证状态
      if (this.$refs.editProductForm) {
        this.$refs.editProductForm.resetFields()
      }
    },

    handleDelete (row) {
      ElMessageBox.confirm(
        '确定要删除该商品吗？',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        // 调用删除接口
        const productId = row.productId // 获取商品ID
        this.$ajax.delete(`/admin/product/deleteByProductId?productId=${encodeURIComponent(productId)}`)
          .then(response => {
            const resp = response.data
            if (resp.code === 0) {
              ElMessage.success('商品删除成功')
              this.getAllProducts() // 删除成功后刷新列表
            } else {
              ElMessage.error(resp.msg || '商品删除失败')
            }
          })
          .catch(error => {
            console.error('删除商品失败:', error)
            ElMessage.error('删除商品失败，请稍后重试')
          })
      }).catch(() => {
        ElMessage.info('已取消删除')
      })
    },

    highlightMatch (text, query) {
      if (!query) {
        return text
      }
      // 尝试将查询字符串转换为数字，用于匹配价格
      const queryNum = parseFloat(query)
      const isPrice = !isNaN(queryNum)

      // 如果是数字，检查是否是价格匹配
      if (isPrice && typeof text === 'number') {
        const priceStr = '¥' + text.toString()
        return priceStr.includes(query)
          ? `<span style="color: red; font-weight: bold;">${priceStr}</span>`
          : priceStr
      }

      // 文本匹配
      const regex = new RegExp(query, 'gi')
      return text.toString().replace(regex, match =>
        `<span style="color: red; font-weight: bold;">${match}</span>`
      )
    },

    // 触发编辑表单中的文件输入
    triggerEditFileInput () {
      this.$refs.editFileInput.click()
    },

    // 处理编辑表单中的图片文件变化
    handleEditImageChange (event) {
      const fileInput = event.target
      if (!fileInput.files.length) return

      const file = fileInput.files[0]
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        ElMessage.error('上传图片只能是 JPG/PNG 格式!')
        fileInput.value = ''
        this.selectedImageFile = null // 清除之前选择的文件
        return
      }
      if (!isLt2M) {
        ElMessage.error('上传图片大小不能超过 2MB!')
        fileInput.value = ''
        this.selectedImageFile = null // 清除之前选择的文件
        return
      }

      // 将校验通过的文件存储到 selectedImageFile 中，暂不上传
      this.selectedImageFile = file

      // 使用 FileReader 生成图片预览
      const reader = new FileReader()
      reader.onloadend = () => {
        this.currentProduct.imageUrl = reader.result
      }
      reader.readAsDataURL(file)

      // 清空文件输入框的值，以便下次选择同一文件也能触发 change 事件
      fileInput.value = ''
    },

    // 修改提交方法，区分新增和编辑
    submitForm () {
      this.$refs.editProductForm.validate((valid) => {
        if (valid) {
          const submitData = {
            productName: this.currentProduct.productName,
            productDesc: this.currentProduct.productDesc,
            productPrice: this.currentProduct.productPrice,
            productStock: this.currentProduct.productStock,
            productSales: this.currentProduct.productSales,
            productClick: this.currentProduct.productClick,
            productStatus: this.currentProduct.productStatus,
            productColor: this.currentProduct.productColor,
            productSize: this.currentProduct.productSize,
            productWeight: this.currentProduct.productWeight,
            productMaterial: this.currentProduct.productMaterial,
            productType: this.currentProduct.productType // 添加分类字段
          }

          // 如果是编辑模式，保留原有的图片路径
          if (this.currentProduct.productId) {
            submitData.productImg = this.currentProduct.productImg
          }

          // 先保存商品信息
          this.saveProduct(submitData)
        }
      })
    },

    // 保存商品信息的通用方法
    saveProduct (submitData) {
      // 根据是否有 productId 判断是新增还是编辑
      const url = this.currentProduct.productId
        ? '/admin/product/updateProductsInfo'
        : '/admin/product/addProduct'

      // 如果是编辑，添加 productId
      if (this.currentProduct.productId) {
        submitData.productId = this.currentProduct.productId
      }

      this.$ajax.post(url, submitData)
        .then(response => {
          if (response.data.code === 0) {
            // 商品信息保存成功后，如果有新的图片文件需要上传
            if (this.selectedImageFile) {
              const formData = new FormData()
              formData.append('photo', this.selectedImageFile)

              // 获取保存后的商品ID
              const productId = response.data.data.productId || this.currentProduct.productId

              // 上传图片
              this.$ajax.post('/photo/upload_photo', formData, {
                headers: { 'Content-Type': 'multipart/form-data' }
              }).then((uploadResponse) => {
                const uploadResp = uploadResponse.data
                if (uploadResp.code === 0) {
                  // 图片上传成功后，更新商品图片信息
                  const updateImgData = {
                    productId: productId,
                    productImg: uploadResp.data
                  }

                  // 更新商品图片信息
                  this.$ajax.post('/admin/product/updateProductsInfo', updateImgData)
                    .then(updateResponse => {
                      if (updateResponse.data.code === 0) {
                        ElMessage.success(this.currentProduct.productId ? '商品信息更新成功' : '商品添加成功')
                        this.dialogVisible = false
                        this.getAllProducts()
                        this.selectedImageFile = null // 清除已上传的文件
                      } else {
                        ElMessage.error('商品图片更新失败')
                      }
                    })
                    .catch(error => {
                      console.error('更新商品图片失败:', error)
                      ElMessage.error('更新商品图片失败，请稍后重试')
                    })
                } else {
                  ElMessage.error(uploadResp.msg || '图片上传失败')
                }
              }).catch(() => {
                ElMessage.error('图片上传失败，请稍后重试')
              })
            } else {
              // 没有新的图片文件，直接提示成功
              ElMessage.success(this.currentProduct.productId ? '商品信息更新成功' : '商品添加成功')
              this.dialogVisible = false
              this.getAllProducts()
            }
          } else {
            ElMessage.error(response.data.msg || (this.currentProduct.productId ? '更新失败' : '添加失败'))
          }
        })
        .catch(error => {
          console.error(this.currentProduct.productId ? '更新失败:' : '添加失败:', error)
          ElMessage.error(this.currentProduct.productId ? '更新失败，请稍后重试' : '添加失败，请稍后重试')
        })
    },

    handlePreview (row) {
      if (row.imageUrl) {
        this.previewUrl = row.imageUrl
        this.previewVisible = true
      }
    },

    // 根据分类ID获取分类名称
    getCategoryName (typeId) {
      const category = this.categories.find(item => item.id === typeId)
      return category ? category.name : '未分类'
    },

    // 修改 selectCategory 方法以适配新的数据格式
    selectCategory () {
      this.$ajax.get('/carousel/category').then(response => {
        const resp = response.data
        if (resp.code === 0) {
          this.categories = resp.data
        } else {
          ElMessage.error(resp.msg || '获取分类列表失败')
        }
      }).catch(error => {
        console.error('获取分类列表失败:', error)
        ElMessage.error('获取分类列表失败，请稍后重试')
      })
    }
  }
}
</script>

<style scoped>
.page-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.main-container {
  display: flex;
  flex: 1;
}

.content-container {
  flex: 1;
  padding: 0;
  background-color: #f5f7fa;
  height: 100%;
  width: 100%;
  overflow-y: auto;
}

.box-card {
  margin-bottom: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-inputs {
  display: flex;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* 添加图片上传相关样式 */
.image-uploader {
  display: inline-block;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: all 0.3s;
}

.image-uploader .product-image {
  width: 120px;
  height: 120px;
  object-fit: cover;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  transition: all 0.3s;
}

.image-uploader-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s;
  border-radius: 4px;
}

.image-uploader:hover .image-uploader-mask {
  opacity: 1;
}

.image-uploader:hover .product-image {
  border-color: #409eff;
  box-shadow: 0 0 8px rgba(64, 158, 255, 0.2);
}

.image-uploader-icon {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  transition: all 0.3s;
}

.upload-text {
  margin-top: 8px;
  font-size: 14px;
  color: #fff;
}

.image-uploader-info {
  margin-top: 8px;
  font-size: 12px;
  color: #90939d;
  text-align: center;
}

.image-uploader-info p {
  margin: 2px 0;
  line-height: 1.5;
}

.image-uploader:hover .image-uploader-icon {
  border-color: #409eff;
  color: #409eff;
}

.image-uploader:hover .upload-text {
  color: #409eff;
}

.image-uploader-mask .upload-text {
  color: #fff;
  margin-top: 0;
}

.image-info {
  margin-top: 10px;
  font-size: 12px;
  color: #606266;
}

.image-tip {
  line-height: 1.5;
  margin: 0;
}

/* 商品图片预览样式 */
.product-preview {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
  border-radius: 4px;
  padding: 4px;
  min-height: 60px;
}

.product-preview :deep(.el-image) {
  border-radius: 4px;
  overflow: hidden;
}

.product-preview :deep(.el-image__inner) {
  transition: transform 0.3s ease;
}

.product-preview :deep(.el-image__inner:hover) {
  transform: scale(1.05);
}

.product-preview :deep(.el-image-viewer__wrapper) {
  background-color: rgba(0, 0, 0, 0.9);
}

.product-preview :deep(.el-image-viewer__btn) {
  color: #fff;
}

.product-preview :deep(.el-image-viewer__actions) {
  background-color: rgba(0, 0, 0, 0.7);
}

.loading-text {
  color: #909399;
  font-size: 14px;
}

.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #909399;
  font-size: 12px;
  height: 100%;
}

.image-error .el-icon {
  font-size: 24px;
  margin-bottom: 4px;
}

/* 图片预览样式优化 */
.preview-image {
  cursor: pointer;
  position: relative;
}

.preview-image:hover::after {
  content: '查看图片';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  border-radius: 4px;
}

:deep(.el-image-viewer__wrapper) {
  background-color: rgba(0, 0, 0, 0.9);
}

:deep(.el-image-viewer__btn) {
  color: #fff;
  font-size: 24px;
  opacity: 0.8;
}

:deep(.el-image-viewer__btn:hover) {
  opacity: 1;
}

:deep(.el-image-viewer__actions) {
  background-color: rgba(0, 0, 0, 0.7);
  border-radius: 4px;
}
</style>
